/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilefrontend.handlers;

import com.vng.zing.logger.ZLogger;
import com.vng.zing.stats.Profiler;
import com.vng.zing.stats.ThreadProfiler;
import com.vng.zing.zprofilefrontend.model.FacebookLoginModel;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author namhh
 */
public class FacebookLogInCallbackHandler extends HttpServlet {

    private static final Logger _Logger = ZLogger.getLogger(FacebookLogInCallbackHandler.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doProcess(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doProcess(req, resp);
    }

    private void doProcess(HttpServletRequest req, HttpServletResponse resp) {
        ThreadProfiler profiler = Profiler.createThreadProfilerInHttpProc("FacebookLogInCallbackHandler", req);
        try {
            FacebookLoginModel.Instance.process(req, resp);
        } catch (Exception ex) {
            _Logger.error(null, ex);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

//    private String getFacebookAccessToken(String faceCode) {
//        String token = null;
//        if (faceCode != null && !"".equals(faceCode)) {
//            String appId = "236551069873118";
//            String redirectUrl = "http://localhost:8000/fblogin";
//            String faceAppSecret = "7c4779e90518531090b5931db7c83bbf";
//            String newUrl = "https://graph.facebook.com/oauth/access_token?client_id="
//                    + appId + "&redirect_uri=" + redirectUrl + "&client_secret="
//                    + faceAppSecret + "&code=" + faceCode;
//            HttpClient httpclient = new DefaultHttpClient();;
//            try {
//                HttpGet httpget = new HttpGet(newUrl);
//                ResponseHandler<String> responseHandler = new BasicResponseHandler();
//                token = httpclient.execute(httpget, responseHandler);
//                token = token.substring(token.indexOf("=") + 1);
//                Long expires = Long.valueOf(token.substring(token.lastIndexOf("=") + 1));
//                token = token.substring(0, token.indexOf("&expires"));
//
//            } catch (ClientProtocolException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                httpclient.getConnectionManager().shutdown();
//            }
//        }
//        return token;
//    }
//
//    private String getProfile(String token) {
//        String profile = null;
//        if (token != null && !"".equals(token)) {
//            String newUrl = "https://graph.facebook.com/me?access_token=" + token;
//            HttpClient httpclient = new DefaultHttpClient();
//            try {
//                HttpGet httpget = new HttpGet(newUrl);
//                ResponseHandler<String> responseHandler = new BasicResponseHandler();
//                profile = httpclient.execute(httpget, responseHandler);
//
//            } catch (ClientProtocolException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                httpclient.getConnectionManager().shutdown();
//            }
//        }
//        return profile;
//    }
//
//    //1. {"id":"248797201983082","email":"development.account\u0040yahoo.com","first_name":"Dev","gender":"male","last_name":"Dev","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/248797201983082\/","locale":"en_US","name":"Dev Dev","timezone":14,"updated_time":"2014-06-05T04:00:47+0000","verified":false}
//    private static Profile mapToProfile(String jsonData) {
//        try {
//            JSONParser parser = new JSONParser();
//            Object obj = parser.parse(jsonData);
//            JSONObject jsonObject = (JSONObject) obj;
//            Profile profile = new Profile();
//            profile.setEmail((String) jsonObject.get("email"));
//            profile.setUsername((String) jsonObject.get("email"));
//            profile.setPassword("");
//            String gender = ((String) jsonObject.get("gender")).toUpperCase();
//            profile.setGender(GENDER.valueOf(gender));
//            profile.setDisplayName((String) jsonObject.get("name"));
//            profile.setRole(ROLE.USER_SOCIAL_FACEBOOK);
//            profile.setStatus(STATUS.ACTIVE);
//            
//            return profile;
//        } catch (ParseException ex) {
//        }
//        return null;
//    }

}
